package com.hp.common.jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.hp.common.tool.JsonTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableConfigurationProperties(JwtProperties.class)
public class JwtToken {
  @Autowired private JwtProperties properties;

  /**
   * 创建token
   *
   * @param userId
   * @param userName
   * @param ext
   * @return
   */
  public String generateToken(Long userId, String userName, Map<String, Object> ext) {
    Date now = new Date();
    Algorithm algorithm = Algorithm.HMAC256(properties.getSecret());
    String token =
        JWT.create()
            .withIssuer(properties.getIssuer())
            .withIssuedAt(now)
            .withExpiresAt(new Date(now.getTime() + properties.getExpireTime()))
            .withClaim("userId", userId)
            .withClaim("userName", userName)
            .withClaim("ext", JsonTool.toJson(ext))
            .sign(algorithm);
    return token;
  }

  /** 验证token */
  public boolean verify(String token) {
    try {
      Algorithm algorithm = Algorithm.HMAC256(properties.getSecret()); // 算法
      JWTVerifier verifier = JWT.require(algorithm).withIssuer(properties.getIssuer()).build();
      verifier.verify(token);
      return true;
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return false;
  }
  /** 从token获取username */
  public String getUserName(String token) {
    try {
      return JWT.decode(token).getClaim("userName").asString();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return "";
  }
  /**
   * token 获取用户id
   *
   * @param token
   * @return
   */
  public Long getUserId(String token) {
    try {
      return JWT.decode(token).getClaim("userId").asLong();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return 0L;
  }

  public Map<String, Object> getExt(String token) {
    try {
      return JsonTool.jsonToMap(JWT.decode(token).getClaim("ext").asString());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return new HashMap<>();
  }
}
